package com.jinghua.treatment.controller;

import com.jinghua.treatment.entity.SysClinic;
import com.jinghua.treatment.entity.SysDoctor;
import com.jinghua.treatment.service.SysClinicService;
import com.jinghua.treatment.service.SysDoctorService;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 门诊信息控制器
 * Created by 王兴光 on 2018/4/18.
 */
@Controller
@RequestMapping("/clinic")
public class SysClinicController {

    @Resource
    private SysClinicService clinicService;

    @Resource
    private SysDoctorService sysDoctorService;

    @ApiOperation(value = "根据医生编号，获取门诊信息列表")
    @GetMapping(value= "/getClinicList", params= {"doctorId"})
    @ResponseBody
    public List<SysClinic> findClinicByDoctorId(Integer doctorId){return clinicService.selectByDoctorId(doctorId);}

    @ApiOperation(value = "修改门诊信息")
    @GetMapping(value= "/updateClinic")
    @ResponseBody
    public int updateClinic(SysClinic clinic){return clinicService.updateByPrimaryKey(clinic);}

    @ApiOperation(value = "删除门诊信息")
    @GetMapping(value= "/updateClinic", params = {"clinicId"})
    @ResponseBody
    public int deleteClinic(Integer clinicId){return clinicService.deleteByPrimaryKey(clinicId);}

    @ApiOperation(value = "添加门诊信息")
    @RequestMapping(value= "/addClinic")
    @ResponseBody
    public int addClinic(SysClinic sysClinic){
        return clinicService.insert(sysClinic);
    }

    @ApiOperation(value = "根据医生编号，获取门诊信息列表")
    @GetMapping(value= "/loadDoctorClinics")
    public String loadDoctorClinics(Integer doctorId,Model model){
        List<SysClinic> clinics = clinicService.selectById(doctorId);
        for(SysClinic sysClinic:clinics){
            String workday = sysClinic.getDescription();
            if(workday!=null&workday.length()>0){
               String  workdayArr[] =  workday.split(",");
               String html="";
               String stage[] ={"上午","下午","晚上"};
               for(int i=0;i<3;i++){
                   html+="<tr align=\"center\">";
                   for(int j=0;j<7;j++){
                       boolean flag = false;
                       for(int k = 0;k<workdayArr.length;k++){
                           if(Integer.parseInt(workdayArr[k])==j*3+i){
                               html+="<td>"+stage[i]+"</td>";
                               flag = true;
                               break;
                           }
                       }
                       if(!flag)
                           html+="<td>&nbsp;</td>";
                   }
                   html+="</tr>";
               }
                sysClinic.setDescription(html);
            }
        }
        model.addAttribute("clinics",clinics);
        return "doctor/clinics";
    }

}
